//
//  MyDetailOrderHeaderView.swift
//  OnceNectar
//
//  Created by 法正磊 on 2018/2/25.
//  Copyright © 2018年 com.inwhoop. All rights reserved.
//
//  我的订单头视图
import UIKit
import SwiftyJSON

class MyDetailOrderHeaderView: UIView, BottomShadow {
    let rightImageViewRightMargin: CGFloat = 23
    let topOffset: CGFloat = 20
    let textRightMargin: CGFloat = 10
    var orderDetailType: OrderDetailType = .waitPay
    func showData(type: OrderDetailType) {
        
        self.backgroundColor = ONColor.cellBGColor
        self.frame = CGRect(x: 0, y: 0, width: ScreenWidth - 20, height: 187)
        
    }
    var timeCount: TimeInterval = 0
    
    var timer: Timer?
    
    var phoneLable: UILabel?
     var addressLable: UILabel?
     var nameLable: UILabel?
     var moneyLable: UILabel?
     var payStatusLable: UILabel?
    var timeLable: UILabel?
    var tipLable: UILabel?
    var model: myOrderModel?{
        didSet{
            
            var type: OrderDetailType
            if model?.status == "0" {
                type = OrderDetailType(rawValue: 0)!
            }else{
                let json = JSON(model?.status)
                type = OrderDetailType(rawValue: json.intValue)!
            }
            orderDetailType = type
            
            setHeaderView()
            phoneLable?.text = model?.telephoneNumber
            nameLable?.text = model?.receiver
            addressLable?.text = model?.deliveryAddres
            if model?.status != nil {
                if let sta: Int = Int((model?.status)!){
                    if (sta == 1 || sta == 2){
                        return
                    }
                }
            }
            
            moneyLable?.text = "￥" + (model?.payAmount)!
            phoneLable?.sizeToFit()
          
            moneyLable?.sizeToFit()
          
            typeUI(type: type, typeLabel: payStatusLable!, rightButton: UIButton())
            payStatusLable?.text = model?.statusName
            let timeCount: Int = Int(self.timeCount)
            if timeCount > 0 {
                self.timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(self.updateCode), userInfo: nil, repeats: true)
                timeLable?.isHidden = false
            }else{
                timeLable?.isHidden = true
            }
        }
        
    }
    
    @objc func updateCode() -> Void {
        var enable: Bool = false
        var title: String?
        let timeCount: Int = Int(self.timeCount)
        if timeCount > 0 {
            let time: String = time2ShowTime(stamp: self.timeCount)
            title = "剩余：" + time
            enable = false
        }else{
            title = ""
            enable = true
            if self.timer != nil && (self.timer?.isValid)! {
                self.timer?.invalidate()
            }
            self.timer = nil
//            self.sendCount = 60
        }
        
        self.timeLable?.text = title
//        self.codeButton.setTitle(title, for: .normal)
//        self.codeButton.isUserInteractionEnabled = enable
        self.timeCount -= TimeInterval(1)
    }
    
    //0:待付款；1：待出库；2：已出库；3：已完成；4：已取消；5：申请退换；6：同意退货；7：拒绝退货
    private func typeUI(type: OrderDetailType, typeLabel: UILabel, rightButton: UIButton) {
        var leftButtonIsHidden = true
        var rightButtonColor = UIColor(hex: 0x8F8F8F)
        var typeTitle = ""
        var typeTitleColor = ONColor.currencyRedColor
        switch type {
        case .waitPay:
            typeTitle = "待付款"
            rightButtonColor = ONColor.currencyRedColor
            rightButton.setTitle("立即付款", for: .normal)
        case .waitOut:
            typeTitle = "待出库"
            rightButtonColor = ONColor.currencyRedColor
            rightButton.setTitle("催单", for: .normal)
        case .out:
            typeTitle = "已出库"
            rightButtonColor = ONColor.currencyRedColor
            leftButtonIsHidden = false
            rightButton.setTitle("确认收货", for: .normal)
        case .done:
            typeTitle = "已完成"
            typeTitleColor = ONColor.normalTextColor
            rightButtonColor = ONColor.currencyRedColor
            rightButton.setTitle("再次购买", for: .normal)
        case .cancel:
            typeTitle = "已取消"
            rightButtonColor = ONColor.currencyRedColor
            rightButton.setTitle("立即付款", for: .normal)
        case .req:
            typeTitle = "申请售后"
            leftButtonIsHidden = false
            rightButtonColor = ONColor.currencyRedColor
            rightButton.setTitle("申请售后", for: .normal)
        case .agree:
            typeTitle = "同意退货"
            typeTitleColor = ONColor.normalTextColor
            rightButton.setTitle("再次购买", for: .normal)
        case .none:
            typeTitle = "拒绝退货"
            typeTitleColor = ONColor.normalTextColor
            rightButton.setTitle("再次购买", for: .normal)
        }
        typeLabel.text = typeTitle
    }
    // MARK: - 头视图设置
    func setHeaderView() {
        
        let orderProgressView = orderProgressViewSet()
        self.addSubview(orderProgressView)
        
        if model?.status != nil {
            if let sta: Int = Int((model?.status)!){
                if (sta == 1 || sta == 2){
                    let tipView = subTipView(topView: orderProgressView)
                    self.addSubview(tipView)
                }else{
                    let topLable = leftTopLable()
                    orderProgressView.addSubview(topLable)
                    let lable0 = rightTopLable().0
                    moneyLable = lable0
                    let lable1 = rightTopLable().1
                    tipLable = lable1
                    let lable2 = rightTopLable().2
                    timeLable = lable2
                    orderProgressView.addSubview(lable0)
                    orderProgressView.addSubview(lable1)
                    orderProgressView.addSubview(lable2)
                //        let tipView = subTipView(topView: orderProgressView)
                //        self.addSubview(tipView)
                
                let consigneeView = setConsigneeView(topView: orderProgressView)
                self.addSubview(consigneeView)
                self.height = consigneeView.bottom
                consigneeView.height += 10
                }
            }
        }else{
       
        let topLable = leftTopLable()
        orderProgressView.addSubview(topLable)
        let lable0 = rightTopLable().0
        moneyLable = lable0
        let lable1 = rightTopLable().1
        tipLable = lable1
        let lable2 = rightTopLable().2
        timeLable = lable2
        orderProgressView.addSubview(lable0)
        orderProgressView.addSubview(lable1)
        orderProgressView.addSubview(lable2)
        }
//        let tipView = subTipView(topView: orderProgressView)
//        self.addSubview(tipView)
        
        let consigneeView = setConsigneeView(topView: orderProgressView)
        self.addSubview(consigneeView)
        self.height = consigneeView.bottom
        consigneeView.height += 10
//        setShadow(view: consigneeView, opacity: bottomShadowOpacity, size: bottomShadowOffset, color: UIColor.black, cornerradius: CORNERRADIUS)
    }
    
    override func layoutSubviews() {
      super.layoutSubviews()
        
        moneyLable?.right = self.width - 30
        tipLable?.right = (moneyLable?.left)! - 3
        timeLable?.width = 100
        timeLable?.right = (moneyLable?.right)!
    }
    func leftTopLable() -> UILabel {
        let lable: UILabel = UILabel(frame: .zero)
        lable.textColor = UIColor(hex: 0xAF3939)
        lable.font = UIFont.systemFont(ofSize: 12)
        lable.left = 15
        lable.top = 15
        lable.text = "已取消"
        lable.sizeToFit()
        payStatusLable = lable
        return lable
    }
    func rightTopLable() -> (right1: UILabel,right2: UILabel, right3: UILabel){
        let lable: UILabel = UILabel(frame: .zero)
        lable.textColor = UIColor.red
        lable.font = UIFont.systemFont(ofSize: 12)
        lable.right = self.width - 30
        lable.bottom = 30
        lable.text = "￥1005.1"
        lable.sizeToFit()
        
        let lable1: UILabel = UILabel(frame: .zero)
        lable1.textColor = UIColor.lightGray
        lable1.font = UIFont.systemFont(ofSize: 10)
        
        lable1.text = "需付款"
        lable1.sizeToFit()
        lable1.bottom = lable.bottom
        lable1.right = lable.left - 3
        
        let lable2: UILabel = UILabel(frame: .zero)
        lable2.textColor = UIColor.lightGray
        lable2.font = UIFont.systemFont(ofSize: 10)
        lable2.textAlignment = .right
        lable2.text = "剩余："
        lable2.sizeToFit()
        lable2.bottom = lable.top - 5
        lable2.right = lable1.right
        
        return (lable, lable1, lable2)
    }
    /// 物流进度
    ///
    /// - Returns: 视图
    func orderProgressViewSet() -> UIView {
        
        let orderProgressView = UIView()
        if model?.status != nil {
            if let sta: Int = Int((model?.status)!){
                if (sta == 1 || sta == 2){
                    let orderProgressView1 = OrderProgressView()
                    orderProgressView1.model = self.model
                    orderProgressView1.backgroundColor = UIColor.white
                    orderProgressView1.frame = CGRect(x: 0, y: 0, width: self.width, height: 70)
                    orderProgressView1.layer.zPosition = 1
                    var datas: [String] = []
                    switch orderDetailType {
                    case .waitPay:
                        datas = ["aa", "bb", "cc"]
                    case .waitOut:
                        datas = ["aa", "bb", "cc"]
                    case .out:
                        datas = ["aa", "bb", "cc", "dd"]
                    case .done:
                        datas = ["aa", "bb", "cc", "dd", "dd"]
                    default:
                        datas = ["aa", "bb", "cc"]
                        break
                    }
                    
                    orderProgressView1.showData(datas: datas)
                    setShadow(view: orderProgressView1, opacity: bottomShadowOpacity, size: bottomShadowOffset, color: UIColor.black, cornerradius: CORNERRADIUS)
                    return orderProgressView1
                }
            }
        }
       
        orderProgressView.backgroundColor = UIColor.white
        orderProgressView.frame = CGRect(x: 0, y: 0, width: self.width, height: 70)
        orderProgressView.layer.zPosition = 1
        var datas: [String] = []
        switch orderDetailType {
        case .waitPay:
            datas = ["aa", "bb", "cc"]
        case .waitOut:
            datas = ["aa", "bb", "cc"]
        case .out:
            datas = ["aa", "bb", "cc", "dd"]
        case .done:
            datas = ["aa", "bb", "cc", "dd", "dd"]
        default:
            datas = ["aa", "bb", "cc"]
            break
        }
        
//        orderProgressView.showData(datas: datas)
        setShadow(view: orderProgressView, opacity: bottomShadowOpacity, size: bottomShadowOffset, color: UIColor.black, cornerradius: CORNERRADIUS)
        return orderProgressView
    }
    
    /// 动态提示
    ///
    /// - Returns: 视图
    func subTipView(topView: UIView) -> UIView {
        
        let redImageViewTopOffset: CGFloat = 15
        let redImageViewLROffset: CGFloat = 15
        let redImageView = UIImageView(image: UIImage(named: "order_red_icon"))
        redImageView.frame.origin = CGPoint(x: redImageViewLROffset, y: topOffset + redImageViewTopOffset)
        
        let label = UILabel()
        label.textColor = UIColor.white
        label.text = "系统处理中"
        label.font = UIFont.systemFont(ofSize: 11)
        label.numberOfLines = 0
        let labelWidht: CGFloat = topView.width - redImageView.width - redImageViewLROffset * 2 - textRightMargin
        
        let labelHeight = MyConfigTool.myConfigWithLabelTextRect(with: CGSize(width: labelWidht, height: CGFloat(MAXFLOAT)), withFontSize: 11, withText: label.text).size.height + 1
        label.frame = CGRect(x: redImageView.right + redImageViewLROffset, y: redImageViewTopOffset + topOffset - 2, width: labelWidht, height: labelHeight)
        
        let tipView = UIView()
        tipView.backgroundColor = ONColor.orederProgressBGColor
        tipView.frame = CGRect(x: 0, y: topView.bottom - topOffset, width: topView.width, height: label.top + label.height + 10)
        tipView.layer.zPosition = 0.9
        setShadow(view: tipView, opacity: bottomShadowOpacity, size: bottomShadowOffset, color: UIColor.black, cornerradius: CORNERRADIUS)
        tipView.addSubview(redImageView)
        tipView.addSubview(label)
        
        if orderDetailType == .waitPay {
            let tap = UITapGestureRecognizer(target: self, action: #selector(tipTap))
            tipView.addGestureRecognizer(tap)
            let rightImageView = UIImageView(image: UIImage(named: "btn_arrow_white_rgiht"))
            rightImageView.sizeToFit()
            rightImageView.frame = CGRect(x: tipView.width - rightImageViewRightMargin - rightImageView.width, y: 0, width: rightImageView.width, height: rightImageView.height)
            rightImageView.centerY = label.centerY
            tipView.addSubview(rightImageView)
        }
        
        return tipView
    }
    
    /// 联系人
    ///
    /// - Parameter topView: 顶部视图
    /// - Returns: 视图
    func setConsigneeView(topView: UIView) -> UIView {
        let lrOffset: CGFloat = 10
        let labelTopOffset: CGFloat = 15
        let consigneeView = UIView()
        consigneeView.backgroundColor = UIColor.white
        setShadow(view: consigneeView, opacity: bottomShadowOpacity, size: bottomShadowOffset, color: UIColor.black, cornerradius: CORNERRADIUS)
        
        let nameLabel = UILabel()
        nameLabel.text = "李老酸"
        nameLabel.textColor = ONColor.normalTextColor
        nameLabel.font = UIFont.systemFont(ofSize: 13)
        nameLabel.sizeToFit()
        nameLabel.frame.origin = CGPoint(x: lrOffset, y: topOffset + labelTopOffset)
        consigneeView.addSubview(nameLabel)
        self.nameLable = nameLabel
        
        let phoneNumberLabel = UILabel()
        phoneNumberLabel.text = MyConfigTool.hide("18888888888", withHead: 3, andTail: 4)
        phoneNumberLabel.textColor = ONColor.subTextColor
        phoneNumberLabel.font = UIFont.systemFont(ofSize: 11)
        phoneNumberLabel.sizeToFit()
        let width = phoneNumberLabel.width
        phoneNumberLabel.width = width + 20
        phoneNumberLabel.left = nameLabel.right + 20
        phoneNumberLabel.centerY = nameLabel.centerY
        
        consigneeView.addSubview(phoneNumberLabel)
        phoneLable = phoneNumberLabel
        
        let addressLabel = UILabel()
        addressLabel.textColor = ONColor.subTextColor
        addressLabel.numberOfLines = 0
        addressLabel.font = UIFont.systemFont(ofSize: 11)
        addressLabel.text = "巴博萨就啥都看见哈山东矿机哈克斯讲得好卡萨丁"
        self.addressLable = addressLabel
        let addressLabelWidht: CGFloat = topView.width - lrOffset  - textRightMargin
        let labelHeight = MyConfigTool.myConfigWithLabelTextRect(with: CGSize(width: addressLabelWidht, height: CGFloat(MAXFLOAT)), withFontSize: 11, withText: addressLabel.text).size.height + 1
        addressLabel.frame = CGRect(x: lrOffset, y: nameLabel.bottom + 5, width: addressLabelWidht, height: labelHeight)
        consigneeView.addSubview(addressLabel)
        consigneeView.frame = CGRect(x: 0, y: topView.bottom + topOffset, width: topView.width, height: addressLabel.bottom + 17)
        let json = JSON(model?.status)
        if json.intValue == 1 || json.intValue == 2{
            
        }else{
            consigneeView.y = topView.bottom
        }
        
//        consigneeView.layer.zPosition = 0.8
        let image:UIImageView = UIImageView(frame: CGRect(x: 0, y: 10, width: ScreenWidth - 20, height: 17))
        image.image = UIImage(named: "bg_cut_shadow")
        consigneeView.addSubview(image)
        image.bottom = addressLabel.bottom + 15
        
        return consigneeView
    }
    
    @objc func tipTap() {
        
    }
}
